import { constantRoutes } from '@/router/index.js'
import { getroutes } from '@/http/api/myHttp'
import Layout from '@/views/layout.vue'
import ParentView from '@/components/ParentView'

/**
 * 循环生成异步路由
 * @Author   HarveyCheng
 * @DateTime 2018-10-09
 * @param    {[type]}    routes [description]
 * @return   {[type]}           [description]
 */
//只有一个作用，把接口中的组件字符串，变成真正的组件对象
function loopCreateRouter(routes) {
  const res = []
  routes.forEach((route) => {
    const tmp = { ...route }
    const tpl = Object.assign({}, tmp)
    if (typeof tmp['component'] !== 'undefined' && tmp['component']) {
      if (tmp['component'] === 'Layout') {
        tpl['component'] = Layout
      } else if (tmp['component'] === 'ParentView') {
        tpl['component'] = ParentView
      } else {
        tpl['component'] = loadView(tmp['component'])
        // tpl['component'] = (resolve) => require([`../../${tmp['component']}`], resolve)
      }
    }
    if (tmp['children']) {
      tpl['children'] = loopCreateRouter(tmp['children'])
    }
    res.push(tpl)
  })
  return res
}
export const loadView = (view) => {
  // 路由懒加载
  return (resolve) => require([`@/${view}`], resolve)
}
const state = {
  routes: [],
  addRoutes: [],
  sidebarRouters: [],
  second_routes: [],
  third_routes: []
}
const getters = {
  GET_Routes(state) {
    return state.routes
  }
}
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.routes = routes
  },
  SET_SIDEBAR_ROUTERS: (state, routes) => {
    state.sidebarRouters = routes
  },
  SET_SECOND_ROUTES: (state, routes) => {
    state.second_routes = routes
  },
  SET_THIRD_ROUTES: (state, routes) => {
    state.third_routes = routes
  }
}
const actions = {
  generateRoutes({ commit }) {
    return new Promise((resolve, reject) => {
      getroutes()
        .then((response) => {
          const data = response.dataList
          const asyncRouterMap = data
          const accessedRoutes = loopCreateRouter(asyncRouterMap)
          accessedRoutes.push({ path: '*', redirect: '/NotFound', hidden: true })
          commit('SET_ROUTES', constantRoutes.concat(accessedRoutes))
          commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(accessedRoutes)) //侧边导航
          resolve(accessedRoutes)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  changeSecondRoutes({ commit, state }, data) {
    commit('SET_SECOND_ROUTES', [])
    const second_routes = state.routes.find((item) => {
      return !item.hidden && (item.path === data.path || (data.path === '' && item.path === '/'))
    })
    if (typeof second_routes !== 'undefined' && second_routes.children) {
      const res = second_routes.children.filter((item) => {
        return !item.hidden && item.path
      })
      if (res.length > 1) {
        commit('SET_SECOND_ROUTES', res)
      }
    }
  },
  changeThirdRoutes({ commit, state }, data) {
    commit('SET_THIRD_ROUTES', [])
    const third_routes = state.second_routes.find((item) => {
      return !item.hidden && (item.path === data.path || (data.path === '' && item.path === '/'))
    })
    if (typeof third_routes !== 'undefined' && third_routes.children) {
      const res = third_routes.children.filter((item) => {
        return !item.hidden && item.path
      })
      if (res.length > 1) {
        commit('SET_THIRD_ROUTES', res)
      }
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
